package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressUpdateParam;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "2.收货地址")
@RestController
@RequestMapping("/address")
public class ReceiverAddressController {
    @Autowired
    private IReceiverAddressService service;

    @ApiOperation("新增地址")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                             @Valid ReceiverAddressAddNewParam addressAddNewParam){
        service.addNew(principal,addressAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除地址")
    @ApiOperationSupport(order = 110)
    @GetMapping("/{id:[0-9]+}/delete-by-id")
    @ApiImplicitParam(name = "id",value = "地址id",required = true, dataType = "long")
    public JsonResult deleteById(@PathVariable @Range(min = 1,message = "请提供有效的地址id") Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改地址")
    @ApiOperationSupport(order = 120)
    @PostMapping("/{id:[0-9]+}/update-by-id")
    @ApiImplicitParam(name = "id",value = "地址id",required = true, dataType = "long")
    public JsonResult updateById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                                 @PathVariable @Range(min = 1,message = "请提供有效的地址id") Long id,
                                 @Valid ReceiverAddressUpdateParam updateParam){
        service.updateById(principal, id, updateParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询地址")
    @ApiOperationSupport(order = 130)
    @GetMapping("/{id:[0-9]+}/select-by-id")
    @ApiImplicitParam(name = "id",value = "地址id",required = true, dataType = "long")
    public JsonResult selectById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                                 @PathVariable @Range(min = 1,message = "请提供有效的地址id") Long id){
        ReceiverAddressStandardVO addressStandardVO = service.selectById(principal, id);
        return JsonResult.ok(addressStandardVO);
    }

    @ApiOperation("根据用户id查询地址列表")
    @ApiOperationSupport(order = 131)
    @GetMapping("/list-by-user-id")
    public JsonResult listByUserId(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal){
        List<ReceiverAddressListItemVO> addressListItemVOS = service.listByUserId(principal);
        return JsonResult.ok(addressListItemVOS);
    }
}
